package com.walter.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.plugins.Page;
import com.walter.common.BaseController;
import com.walter.common.PageVO;
import com.walter.common.Result;
import com.walter.mapper.LogMapper;
import com.walter.model.query.LogQuery;
import com.walter.model.vo.LogVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：ch
 * @date ：Created in 2021/5/25
 * @description：
 * @modified By：
 * @version: 1.0
 */
@RestController
@RequestMapping("/api/log")
@Api(tags = "系统日志")
public class LogController extends BaseController {
    @Autowired
    private LogMapper logMapper;

    @PostMapping("find")
    @ApiOperation("分页查询系统日志(用户id、用户操作、请求方法、ip地址、时间)")
    public Result<PageVO<LogVO>> find(@RequestBody LogQuery query){
        Page<LogVO> page = new Page<>(query.getPage().getPageNum(),query.getPage().getPageSize());
        List<LogVO> list = logMapper.find(page,BeanUtil.beanToMap(query));
        //为了让前端展示好看点,把请求参数中的空格符换成相应的&nbsp;  换行符(\n)换成 <br/>
        list = list.stream().map(logVO ->
                logVO.setPrettyParams(null != logVO.getParams() ?
                        logVO.getParams().replaceAll(" ","&nbsp;")
                                .replaceAll("\\n","<br/>") : null)).collect(Collectors.toList());
        return success(new PageVO<>(page.setRecords(list)));
    }
}
